﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using JasRibbonApplicatie.Views;
using JasRibbonApplicatie.Classes;
using JasRibbonApplicatie.Views.BeheerAanvragen;
using System.Windows.Threading;
using JasRibbonApplicatie.Model;

namespace JasRibbonApplicatie
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : RibbonWindow
	{
        // geen constanten -- waarom hoofdletters?
		private LinearGradientBrush SELECTED_BGCOLOR = new LinearGradientBrush();
		private Brush SELECTED_BORDER_COLOR = Brushes.Orange;
		private Login login = null;
		public Page currentPage;
		public Aanvraag SelectedAanvraag { private set; get; }
        public bool? Inleveren
        {
            get
            {
                if (btnAfhalen.Background == null && btnInleveren.Background == null)
                    return null;
                else
                    return (btnAfhalen.Background == null && btnInleveren.Background != null);

            }
        }
        private static AanvraagModel am = new AanvraagModel();

		public MainWindow()
		{
			InitializeComponent();
			
			#region init selection brush
			SELECTED_BGCOLOR.GradientStops.Add(new GradientStop(Color.FromRgb(255, 253, 249), 0.0));
			SELECTED_BGCOLOR.GradientStops.Add(new GradientStop(Color.FromRgb(253, 222, 185), 0.39));
			SELECTED_BGCOLOR.GradientStops.Add(new GradientStop(Color.FromRgb(255, 207, 111), 0.40));
			SELECTED_BGCOLOR.GradientStops.Add(new GradientStop(Color.FromRgb(255, 234, 168), 1.0));
			SELECTED_BGCOLOR.Opacity = 60;
			SELECTED_BGCOLOR.StartPoint = new Point(0, 0);
			SELECTED_BGCOLOR.EndPoint = new Point(0, 1);
			#endregion
            
			login = Login.Instance;
			login.MainWindowReference = this;
			Aanmelden();
			//onLoadCompleted => methode uitvoeren die content set in currentPage dit om vanaf de ribon een page te kunnen "besturen"
			mainFrame.NavigationService.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(NavigationService_LoadCompleted);
		}
		
		// Summary:
		//      Closes the entire program
		//
        private void CloseButton_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		// Summary:
		//      Closes the main application and opens the login window, so that a new user can login.
		//
		public void Logoff_Click(object sender, RoutedEventArgs e)
		{
			login.UserData.save();
			login.userLogOff();
			Aanmelden();
		}

		public void Aanmelden()
		{
			if (login.isLoggedIn)
			{
                scannenTab.IsSelected = true;
                //scannenTab.RaiseEvent(new RoutedEventArgs(;
                materiaalTab.Visibility = Visibility.Hidden;
                aanvragenTab.Visibility = Visibility.Hidden;
                gebruikersTab.Visibility = Visibility.Hidden;
                planningTab.Visibility = Visibility.Hidden;

                if (login.IsBeheerder)
                {
                    materiaalTab.Visibility = Visibility.Visible;
                    aanvragenTab.Visibility = Visibility.Visible;
                    gebruikersTab.Visibility = Visibility.Visible;
                    planningTab.Visibility = Visibility.Visible;
                    materiaalTab.IsEnabled = true;
                    aanvragenTab.IsEnabled = true;
                    gebruikersTab.IsEnabled = true;
                    planningTab.IsEnabled = true;
                }
				Logoff.IsEnabled = true;
				scannenTab.IsEnabled = true;

                mainFrame.Visibility = Visibility.Collapsed;
                navigationProgressBar.Value = 0;
                navigationStatusGrid.Visibility = Visibility.Visible;
                mainFrame.Source = new Uri("Views/Ontleningen/KiesJeAanvraag.xaml", UriKind.Relative);
				this.Show();
			}
			else
			{
				Logoff.IsEnabled = false;
				scannenTab.IsEnabled = true;
				materiaalTab.IsEnabled = true;
				aanvragenTab.IsEnabled = false;
				gebruikersTab.IsEnabled = false;
				planningTab.IsEnabled = false;
				this.Hide();
				AanmeldWindow aanmeldWindow = new AanmeldWindow(this);
				aanmeldWindow.ShowDialog();
			}
		}

        //sets current page after page is loaded
		void NavigationService_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
		{
			currentPage = ((Page)e.Content);
		}

        // nodig voor ontleningen (default button aanzetten)
        public LinearGradientBrush GetButtonBrush()
        {
            return SELECTED_BGCOLOR;
        }

        //default butten click handler
        private void onButton_Click(object sender, RoutedEventArgs e)
        {
            String buttonName = ((Button)(sender)).Name;
            String uri = string.Empty;

            switch (buttonName)
            {
                case "btnNewItem":
                    uri = "Views/BeheerMateriaal/NieuwItemBarcodeToewijzen.xaml";
                    break;
                case "btnRefreshItem":
                    uri = "Views/BeheerMateriaal/BarcodeVernieuwen.xaml";
                    break;
                case "btnPrintLabels":
                    uri = "Views/BeheerMateriaal/PrintLabels.xaml";
                    break;
                case "gebruikersLijstButton":
                    uri = "Views/BeheerGebruikers.xaml";
                    break;
                case "planningButton":
                    uri = "Views/MagazijniersPlanning.xaml";
                    break;
                default:
                    uri = "Views/Home.xaml";
                    break;
            }
            mainFrame.Visibility = Visibility.Collapsed;
            navigationProgressBar.Value = 0;
            navigationStatusGrid.Visibility = Visibility.Visible;
            mainFrame.Source = new Uri(uri, UriKind.Relative);
        }
        //tab click handler
        private void onTab_Click(object sender, SelectionChangedEventArgs e)
        {
            Ribbon r = (Ribbon)sender;
            RibbonTab rt = (RibbonTab)r.SelectedItem;
            String uri = string.Empty;

            switch (rt.Name)
            {
                case "scannenTab":
                    uri = "Views/Ontleningen/KiesJeAanvraag.xaml";
                    break;
                case "materiaalTab":
                    uri = "Views/BeheerMateriaal/BeheerMateriaal.xaml";
                    break;
                case "aanvragenTab":
                    uri = "Views/BeheerAanvragen/OverzichtAanvragen.xaml";
                    break;
                case "GebruikersTab":
                    uri = "Views/BeheerGebruikers.xaml";
                    break;
                case "PlanningTab":
                    uri = "Views/MagazijniersPlanning.xaml";
                    break;
            }
            if (uri != string.Empty)
            {
                mainFrame.Visibility = Visibility.Collapsed;
                navigationProgressBar.Value = 0;
                navigationStatusGrid.Visibility = Visibility.Visible;
                mainFrame.Source = new Uri(uri, UriKind.Relative);
            }

        }
        //OntleningButtons click handler
		private void onOntleningButton_Click(object sender, RoutedEventArgs e)
		{
			Button button = ((Button)sender);
			switch (button.Name)
			{
                case "btnInleveren":
                    // visuele reactie op wat geselecteerd is...
                    setVisualSelectionOntleningsButtons(true,currentPage);
					break;
                case "btnAfhalen":
				default:
                    // visuele reactie op wat geselecteerd is...
                    setVisualSelectionOntleningsButtons(false, currentPage);
					break;
			}
		}
        //overzichtMateriaal click handler
		private void overzichtMateriaalStatus_Click(object sender, RoutedEventArgs e)
		{
			Button btn = (Button)(sender);
			BeheerMateriaal bm = currentPage as BeheerMateriaal;
            if (bm != null) bm.ButtonChoice(btn.Name);
            else
            {
                mainFrame.Visibility = Visibility.Collapsed;
                navigationProgressBar.Value = 0;
                navigationStatusGrid.Visibility = Visibility.Visible;
                mainFrame.Source = new Uri("Views/BeheerMateriaal/BeheerMateriaal.xaml", UriKind.Relative);
            }
		}
        //aanvraagFlter click handler
		private void aanvraagFilterStatus_Click(object sender, RoutedEventArgs e)
		{
			Button btn = (Button)(sender);
			
			var ba = currentPage as OverzichtAanvragen;
			Status status;
			switch (btn.Name)
			{
				case "nieuweAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "nieuw").First();
					break;
				case "eersteGoedkeuringAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "eerste goedkeuring").First();
					break;
				case "defGoedkeuringAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "definietieve goedkeuring").First();
					break;
				case "materiaalBuitenAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "materiaal buiten").First();
					break;
                case "afgerondAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "ontlening afgerond").First();
					break;
				case "afgekeurdeAanvragenButton":
                    status = am.GetAllStatussen().Where(s => s.status == "afgekeurd").First();
					break;
				default:
					status = null;
					break;
			}
			if (btn.Background != SELECTED_BGCOLOR)
			{
				ba.addToStatusFilter(status);
				btn.Background = SELECTED_BGCOLOR;
				btn.BorderBrush = SELECTED_BORDER_COLOR;
			}
			else
			{
				ba.RemoveFromStatusFilter(status);
				btn.Background = null;
				btn.BorderBrush = null;
			}
		}
        //aanvraagTools click handler
		private void AanvragenTools_Click(object sender, RoutedEventArgs e)
		{
			Button btn = (Button)(sender);
			string uri = string.Empty;

			switch (btn.Name)
			{
				case "AanvraagKiezenButton":
					if (currentPage.GetType() != typeof(OverzichtAanvragen)){
						SelectedAanvraag = null;
						uri = "Views/BeheerAanvragen/OverzichtAanvragen.xaml";
					}
					break;
				case "AanvraagMateriaalButton":
					if (currentPage.GetType() == typeof(OverzichtAanvragen))
					{
						OverzichtAanvragen p = currentPage as OverzichtAanvragen;
						SelectedAanvraag = p.selectedAanvraag;
					}
					if (SelectedAanvraag != null) uri = "Views/BeheerAanvragen/MateriaalAanvragen.xaml";
					break;
				case "AanvraagMailsButton":
					if (currentPage.GetType() == typeof(OverzichtAanvragen))
					{
						OverzichtAanvragen p = currentPage as OverzichtAanvragen;
						SelectedAanvraag = p.selectedAanvraag;
					}
					if (SelectedAanvraag != null) uri = "Views/BeheerAanvragen/MailsAanvragen.xaml";
					break;
				case "AanvraagFormulierenButton":
					if (currentPage.GetType() == typeof(OverzichtAanvragen))
					{
						OverzichtAanvragen p = currentPage as OverzichtAanvragen;
						SelectedAanvraag = p.selectedAanvraag;
					}
					if (SelectedAanvraag != null) uri = "Views/BeheerAanvragen/FormulierenAanvragen.xaml";
					break;
			}
            if (uri != string.Empty)
            {
                mainFrame.Visibility = Visibility.Collapsed;
                navigationProgressBar.Value = 0;
                navigationStatusGrid.Visibility = Visibility.Visible;
                mainFrame.Source = new Uri(uri, UriKind.Relative);
            }
		}

        private void MailTools_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)(sender);
			string uri = string.Empty;

            if (currentPage.GetType() == typeof(MailsAanvragen))
            {
                switch (btn.Name)
                {
                    case "nieuweMail":
                        MailsAanvragen p = currentPage as MailsAanvragen;
                        p.NewMail();
                        break;

                }
            }

            
        }

		private void btnScanActie_Click(object sender, RoutedEventArgs e)
		{
            Button button = (Button)sender;

            if (currentPage.GetType() == typeof(KiesJeAanvraag))
            {
                var huidigePagina = currentPage as KiesJeAanvraag;
                if (button.Name.Equals("btnHuidigeAanvragen"))
                {
                    huidigePagina.AanvragenVandaagButtonClick();
                }
            }
            else if (currentPage.GetType() == typeof(OntleningenView))
            {
                var huidigePagina = currentPage as OntleningenView;
                if (button.Name.Equals("btnOntleningBeheer"))
                {
                    huidigePagina.ontleningBeherenButton_Click();
                }
                else if (button.Name.Equals("btnOngendaanMaken"))
                {
                    huidigePagina.undo();// nog undo implementeren
                }
            }
            
            
            
		}

        public void gotoScannen(Aanvraag aanvraag)
        {
            SelectedAanvraag = aanvraag;

            ScannenGroup.IsEnabled = false;
            OntleningsGroup.IsEnabled = true;
            OntleningsGroup.Visibility = Visibility.Visible;

            mainFrame.Source = new Uri("Views/Ontleningen/Scannen.xaml", UriKind.Relative);
        }


        private void mainFrame_NavigationProgress(object sender, NavigationProgressEventArgs e)
        {
            long progress = e.BytesRead * 100 / e.MaxBytes;
            Console.WriteLine("Navigating progress:" + progress + "%");
            navigationProgressBar.Value = progress;
        }

        private void mainFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            Console.WriteLine("start navigating");
            mainFrame.Visibility = Visibility.Hidden;
            navigationStatusGrid.Visibility= Visibility.Visible;
        }

        private void mainFrame_Navigated(object sender, NavigationEventArgs e)
        {
            Console.WriteLine("end navigating");
            mainFrame.Visibility = Visibility.Visible;
            navigationStatusGrid.Visibility = Visibility.Hidden;
        }


        internal void displayError(string p)
        {
            throw new NotImplementedException();
        }

        public void setVisualSelectionOntleningsButtons(bool? inlevering, Page cPage)
        {
            if (inlevering == null)
            {
                this.btnAfhalen.Background = null;
                this.btnAfhalen.BorderBrush = null;
                this.btnInleveren.Background = null;
                this.btnInleveren.BorderBrush = null;
            }
            else if (inlevering.Value)
            {
                this.btnAfhalen.Background = null;
                this.btnAfhalen.BorderBrush = null;
                this.btnInleveren.Background = SELECTED_BGCOLOR;
                this.btnInleveren.BorderBrush = Brushes.Crimson;
            }
            else
            {
                this.btnInleveren.Background = null;
                this.btnInleveren.BorderBrush = null;
                this.btnAfhalen.Background = SELECTED_BGCOLOR;
                this.btnAfhalen.BorderBrush = Brushes.Crimson;
            }

            if (cPage.GetType() == typeof(KiesJeAanvraag))
            {
                var huidigePagina = cPage as KiesJeAanvraag;
                huidigePagina.Inlevering = inlevering;// zodat de functionaliteit aangepast wordt...
            }
        }
    }
}
